package service.impl;

import java.util.ArrayList;
import java.util.List;

import dao.DishDao;
import dao.OrderDao;
import dao.impl.DishDaoImpl;
import dao.impl.OrderDaoImpl;
import po.Dish;
import po.Order;
import po.OrderDetail;
import service.OrderService;

public class OrderServiceImpl implements OrderService{

	@Override
	public int addOrder(Order o,int cId) {
		OrderDao orderDao = new OrderDaoImpl();
		int m = orderDao.addOrder(o, cId);
		int n = orderDao.addOrderDish(o, cId);
		if(m==1){
			if(n == 1){
				return 1;
			}else{
				System.out.println("add orderDish failed");
				return 0;
			}			
		}else{
			System.out.println("add order failed");
			return 0;
		}
	}

	@Override
	public int delOrder(String oId) {
		OrderDao orderDaoImpl = new OrderDaoImpl();
		int m = orderDaoImpl.delOrder(oId);
		int n = orderDaoImpl.delOrderDish(oId);
		if(m == 1){
			if(n == 1){
				return 1;
			}else{
				System.out.println("del orderDish failed");
				return 0;
			}
		}else{
			System.out.println("del order failed");
			return 0;
		}
		
	}

	@Override
	public List<Dish> Query(int cId) {
		OrderDao orderDaoImpl = new OrderDaoImpl();
		DishDao dishDaoImpl = new DishDaoImpl();
		List<Order> orderList = new ArrayList<Order>();
		orderList = orderDaoImpl.queryOrder(cId);
		List<Dish> dishList = new ArrayList<Dish>();
		for(Order o:orderList){
			int dId = o.getdId();
			Dish dish = dishDaoImpl.queryADish(dId);
			dish.setoId(o.getoId());
			dish.setNum(o.getSumNum());
			dish.setSumPrice(o.getSumPrice());
			dishList.add(dish);			
		}
		return dishList;
	}

	@Override
	public List<Dish> QueryDetail(String oId) {
		OrderDao orderDaoImpl = new OrderDaoImpl();
		DishDao dishDaoImpl = new DishDaoImpl();
		List<OrderDetail> orderDetailList = new ArrayList<OrderDetail>();
		orderDetailList = orderDaoImpl.queryOrderDish(oId);
		List<Dish> dishList = new ArrayList<Dish>();
		for(OrderDetail od:orderDetailList){
			int dId = od.getdId();
			Dish dish = dishDaoImpl.queryADish(dId);
			dish.setNum(od.getNum());
			dish.setOdId(od.getOdId());
			dishList.add(dish);
		}
		return dishList;
	}

}
